import sys
import time
import pytest

from PO项目实战.驱动层.driver_config import WebdriverConf
from PO项目实战.业务层.login_business import LoginBusiness
from PO项目实战.业务层.user_business import UserBusiness
from PO项目实战.业务层.iframe_business import IframeBusiness
from PO项目实战.业务层.upload_file_business import UploadFileBusiness


@pytest.fixture(scope="function")
def login_fixture():
    """
    登录测试用例专用夹具，登录成功执行退出操作再关闭浏览器，登录不成功直接关闭浏览器
    :return:
    """
    driver_config = WebdriverConf()
    driver = driver_config.driver
    driver.maximize_window()
    login_business = LoginBusiness(driver)
    yield login_business
    time.sleep(1)
    # 退出登录
    if "系统首页" in login_business.get_title():
        login_business.do_logout()
    # 关闭浏览器
    login_business.driver.quit()

# 夹具，session表示整个测试会话值调用一次
# 由于login测试用例不需要使用此夹具，所以不设置夹具的autouse=True
@pytest.fixture(scope="session")
def setup_teardown_session():
    print("前置条件：用户登录！！！")
    # 获取driver
    driver_config = WebdriverConf()
    driver = driver_config.driver
    if driver is None:
        print("输入浏览器名字错误！")
        sys.exit(1)
    else:
        # 登录
        # driver.get('http://localhost:8080/Login')
        driver.get('http://wj53793py34.vicp.fun/Login')
        driver.maximize_window()
        login_business = LoginBusiness(driver)
        login_business.do_login("admin", "123456")
        # 登录不成功，再点击一下登录按钮
        time.sleep(2)
        if "登录" in login_business.get_title():
            login_business.button_ele.click()
    yield driver    # 暂停执行并返回 driver 实例给测试用例方法
    print("后置条件：用户退出！！！")
    # 关闭浏览器
    # driver.quit()

@pytest.fixture(scope="class")
def user_fixture(setup_teardown_session):
    driver = setup_teardown_session
    user_business = UserBusiness(driver)
    yield user_business

@pytest.fixture(scope="class")
def iframe_fixture(setup_teardown_session):
    driver = setup_teardown_session
    iframe_business = IframeBusiness(driver)
    yield iframe_business

@pytest.fixture(scope="class")
def upload_file_fixture(setup_teardown_session):
    driver = setup_teardown_session
    upload_file_business = UploadFileBusiness(driver)
    yield upload_file_business